package cn.itcast.bos.service.action.teke_delivary;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivary.WayBill;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.bos.web.Dao.teke_delivary.WayBillService;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {
	// 日志 对 程序运行进行的记录
	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);
	@Autowired
	private WayBillService wayBillService;

	@Action(value = "wayBill_save", results = { @Result(name = "success", type = "json") })
	public String save() {

		Map<String, Object> result = new HashMap<>();

		try {
			// 去除没有id 的orderduixang
			if (model.getOrder() != null && model.getOrder().getId() == null || model.getOrder().getId() == 0) {
				// 去除无用对象的关联
				model.setOrder(null);

			}

			// 调用业务层 进行快速录入订单的报存
			wayBillService.save(model);
			// 保存成功 将运单保存成功的信息进行保存 将其返回前台页面
			result.put("success", true);
			result.put("msg","保存成功");

			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());

		} catch (Exception e) {

			e.printStackTrace();
			// 保存失败
			result.put("success", false);
			System.out.println(model.getWayBillNum() + ".............................");
			result.put("msg", "保存失败");
			LOGGER.error("保存运单失败，运单号：" + model.getWayBillNum(), e);
		}
		// 压入值栈
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	// 进行运单列表的展示
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 无条件查询 将请求页数 和 每页展示数据 进行封装
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		// 调用业务层    进行查询
		Page<WayBill> pageData = wayBillService.findPageData(model,pageable);
		// 将查询结果压入 值栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	// 进行快速录入数据的回显
	@Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum(){
		// 调动业务层 进行查询
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		// 对查询结果进行判断
		Map<String, Object> result = new HashMap<String, Object>();
		if (wayBill == null) {
			// 运单不存在
			result.put("success", false);
		} else {
			// 运单存在 将查询到的数据进行回显
			result.put("success", true);
			result.put("wayBillData", wayBill);
		}
		// 压入值栈
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

}
